package com.niguang.common.mybatis;

import com.baomidou.mybatisplus.enums.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DbType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.ArrayList;

/**
 * <p>
 * 测试生成代码
 * *  * </p>
 * *  *
 */
public class Generator {

    /**
     * 表前缀
     */
    public static final String TABLE_PREFIX = "work_";

    public static final String PACKAGE_NAME = "com.niguang.work";

    public static final String profile = "dev";


    public static void main(String[] args) {
        String[] TABLE_NAME = {"device_wechat_user"};

        generateByTables(TABLE_NAME);
    }


    public static void generateByTables(String[] tableName) {
        GlobalConfig globalConfig = new GlobalConfig();
        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setCapitalMode(true)
                .setEntityLombokModel(false)
                //.setDbColumnUnderline(true)
                .setRestControllerStyle(true)
                .setEntityColumnConstant(true)
                .setTablePrefix(TABLE_PREFIX)
                .setNaming(NamingStrategy.underline_to_camel)
                .setLogicDeleteFieldName("del_flag")
                .setTableFillList(new ArrayList<TableFill>() {{
                    add(new TableFill("create_time", FieldFill.INSERT));
                    add(new TableFill("update_time", FieldFill.INSERT_UPDATE));
                }})
                .setInclude(tableName);
        String srcPath = "src" + File.separator + "main" + File.separator + "java";
        globalConfig.setActiveRecord(true)
                .setAuthor(System.getProperty("user.name"))
                .setOutputDir(srcPath)
                .setEnableCache(false)
                .setBaseResultMap(true)
                .setBaseColumnList(false)
                .setKotlin(false)
                .setActiveRecord(true)
                .setFileOverride(true);
        //.setServiceName("%sService");
        new AutoGenerator().setGlobalConfig(globalConfig)
                .setDataSource(StringUtils.equalsAnyIgnoreCase(profile, "dev") ? getDevDb() : getProdDb())
                .setStrategy(strategyConfig)
                .setPackageInfo(new PackageConfig()
                        .setParent(PACKAGE_NAME)
                        .setController("controller")
                        .setEntity("entity")
                        .setModuleName("api")
                ).execute();
    }


    private static DataSourceConfig getProdDb() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        String dbUrl = "jdbc:mysql://47.106.194.24:3306/work?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("devops")
                .setPassword("Ng@2018!@#").setDriverName("com.mysql.jdbc.Driver");
        return dataSourceConfig;
    }

    private static DataSourceConfig getDevDb() {
        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        String dbUrl = "jdbc:mysql://120.79.229.170:3306/work?useUnicode=true&useSSL=false&characterEncoding=utf8&serverTimezone=Asia/Shanghai";
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setUsername("root")
                .setPassword("Niguang@2018").setDriverName("com.mysql.jdbc.Driver");
        return dataSourceConfig;
    }


}
